//
//  HallViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-11.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "HallViewController.h"
#import "HallCell.h"
#import "SeatConfirmViewController.h"
#import "LoginViewController.h"

@interface HallViewController ()
{
    int totalCount;
    int lastCount;
    int state;
    NSString *message;
}

@end

@implementation HallViewController


- (id)initWithURL:(NSString *)url
{
    self = [super initWithNibClass:[HallViewController class]];
    if (self) {
        _dataSource = [[HallDataSource alloc] initWithVC:self];
        [_dataSource fetchSeatListWithURL:url];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:_seatType == Lounge ? @"雅座" : @"大厅"];
    
    self.hallTV.delegate = self.dataSource;
    self.hallTV.dataSource = self.dataSource;
    
    [UIView drawBorders:@[_topView,_downView]];
    
    [self.orderTimeBtn setTitleForNormal:_orderTime];
    
    if (_seatState == 2) {
        [_btnDone setTitleColorNormal:[UIColor grayColor]];
        [_btnDone setBackgroundNormal:@"btn_enable.png"];
    }
}

- (IBAction)leftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)selectedFinished
{
    totalCount = 0;
    
    for (NSDictionary *dic in _dataSource.seatList) {
        int count = [[dic objectForKey:@"selected"] intValue];
        totalCount += count;
    }
    
    self.totalCountLabel.text = [NSString stringWithFormat:@"已选择%d个座位",totalCount];
}

- (IBAction)doneAction:(id)sender
{
    if (state == 2) {
        [AppDelegate showMessage:message];
        return;
    }
    
    if (![self isSelectedSeat]) {
        [AppDelegate showMessage:@"请选择需要预定的座位！"];
        return;
    }
    
    if (![AppDelegate checkLoginWithDelegate:self]) return;
    
    [_dataSource.seatEntity setObject:_dataSource.seatList forKey:@"zuowei"];
    
    SeatConfirmViewController *confirmVC = [[SeatConfirmViewController alloc] initWithShopId:_shopId];
    confirmVC.shopEntity = self.data;
    confirmVC.seatEntity = _dataSource.seatEntity;
    
    [self.navigationController pushViewController:confirmVC animated:YES];
}

- (void)updateViews:(NSDictionary*)dictionary
{
    NSLog(@"%@", dictionary);
    
    lastCount = [[dictionary objectForKey:@"shengyu"] intValue];
    state = [[dictionary objectForKey:@"zhuangtai"] intValue];
    message = [dictionary objectForKey:@"shuoming"];
    
    NSString *lastText = [NSString stringWithFormat:@"剩余：%d",lastCount];
    
    [self createRightButtonWithTitle:lastText background:@"" frame:CGRectMake(0, 0, 56, 30)];
    
    if (state == 2) {
        [_btnDone setTitleColorNormal:[UIColor grayColor]];
        [_btnDone setBackgroundNormal:@"btn_enable.png"];
    }
}

- (BOOL)isSelectedSeat
{
    for (NSDictionary *dic in _dataSource.seatList) {
        if ([[dic objectForKey:@"selected"] intValue] == 1) {
            return YES;
        }
    }
    return NO;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    loginVC.backController = self;
    [self.navigationController pushViewController:loginVC animated:YES];
}

@end

//=========================================================================================
#pragma=================数据源实现===========================================================
//=========================================================================================
@implementation HallDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
        _seatList = [[NSMutableArray alloc] initWithCapacity:10];
    }
    
    return self;
}

- (void)fetchSeatListWithURL:(NSString*)url
{
    [SVProgressHUD show];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    [SVProgressHUD dismiss];
    
    NSDictionary *json = [request.responseData JSONValue];
    
    [self.viewController updateViews:json];
    self.seatList = [json objectForKey:@"zuowei"];
    
    for (NSMutableDictionary *dic in _seatList) {
        [dic setObject:@"0" forKey:@"selected"];
    }
    
    [self.viewController.hallTV reloadData];
    self.seatEntity = [NSMutableDictionary dictionaryWithDictionary:json];
}

#pragma=====================================tableview======================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _seatList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellId = @"HallCell";
    HallCell *cell = [_viewController createCellWithNib:CellId tableview:tableView];
    
    NSMutableDictionary *dictionary = [_seatList objectAtIndex:indexPath.row];
    [cell setContent:dictionary];
    cell.delegate = _viewController;
    
    return cell;
}

@end

